package net.cyan.arachne.exts;

import net.cyan.arachne.result.XmlResult;
import net.cyan.commons.util.XmlUtils;
import org.w3c.dom.*;

import java.util.*;

/**
 * rss
 *
 * @author camel
 * @date 2009-3-2
 */
public class Rss implements XmlResult
{
    private String title = "";

    private String link = "";

    private String description = "";

    private String language = "";

    private String generator = "";

    private String copyright = "";

    private String charset;

    private List<RssItem> items = new ArrayList<RssItem>();

    public Rss()
    {
    }

    public Document toDocument() throws Exception
    {
        Document document = XmlUtils.createDocument();

        Element rssElement = document.createElement("rss");
        rssElement.setAttribute("version", "2.0");
        document.appendChild(rssElement);

        Element channelElement = document.createElement("channel");
        rssElement.appendChild(channelElement);

        XmlUtils.appendChildElement(channelElement, "title", title, document);
        XmlUtils.appendChildElement(channelElement, "link", link, document);
        XmlUtils.appendChildElement(channelElement, "language", language, document);
        XmlUtils.appendChildElement(channelElement, "description", description, document);
        XmlUtils.appendChildElement(channelElement, "generator", generator, document);
        XmlUtils.appendChildElement(channelElement, "copyright", copyright, document);

        for (RssItem item : items)
            channelElement.appendChild(toElement(item, document));

        return document;
    }

    public Element toElement(RssItem item, Document document)
    {
        Element element = document.createElement("item");

        XmlUtils.appendChildElement(element, "title", item.getTitle(), document);
        XmlUtils.appendChildElement(element, "link", item.getLink(), document);
        XmlUtils.appendChildElement(element, "category", item.getCategory(), document);
        XmlUtils.appendCDATAChildElement(element, "description", item.getDescription(), document);
        XmlUtils.appendChildElement(element, "author", item.getAuthor(), document);
        XmlUtils.appendChildElement(element, "pubDate", item.getPubDate(), document);

        return element;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    public String getLink()
    {
        return link;
    }

    public void setLink(String link)
    {
        this.link = link;
    }

    public String getDescription()
    {
        return description;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

    public String getLanguage()
    {
        return language;
    }

    public void setLanguage(String language)
    {
        this.language = language;
    }

    public String getGenerator()
    {
        return generator;
    }

    public void setGenerator(String generator)
    {
        this.generator = generator;
    }

    public String getCopyright()
    {
        return copyright;
    }

    public void setCopyright(String copyright)
    {
        this.copyright = copyright;
    }

    public String getCharset()
    {
        return charset;
    }

    public void setCharset(String charset)
    {
        this.charset = charset;
    }

    public List<RssItem> getItems()
    {
        return items;
    }

    public void addItem(RssItem item)
    {
        items.add(item);
    }
}
